<?php

namespace App\Models\User;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\User\Traits\Relationship\UserRelationship;
use App\Models\User\UserExt;
use Illuminate\Support\Facades\DB;

class Users extends Authenticatable
{
    use UserRelationship;
    //填充字段
    protected $fillable = ['id','name','email','password'];

    //用户状态
    const ACTIVE = 1;

    //用户来源
    const EXT = 0;
    /**
     * 修改记录
     * @param mixed $request
     * @return bool
     */
    public function updateRow ($request) {
        $user = $this->find($request->id);
        //update user
        $user->real_name = $request->real_name;
        $user->email = encrypt_func($request->email,$this->name);
        $user->phone = encrypt_func($request->phone,$this->name);
        return DB::transaction(function () use ($user, $request) {
            //用户扩展
            $ext = [
                'user_id' => $request->id,
                'alias' => $request->alias,
                'birth' => $request->birth,
                'team' => $request->team,
                'wechat' => $request->wechat,
                'qq' => $request->qq,
                'home_page' => $request->home_page,
                'sex' => $request->sex,
                'avatar' => $request->uploadedAvatar
            ];
            if ($userExt = UserExt::where(['user_id'=>$request->id])->first()){

                $userExt->where(['user_id'=>$request->id])->update($ext);
            } else {
                $userExt = new UserExt();

                $userExt->create($ext);
            }
            // UserExt::updateOrCreate(['user_id'=>$request->id],$ext);
            if ($user->save()) {
                return true;
            }
            return false;
        });

    }
    /**
     * 插入记录
     * @param mixed $data
     * @return bool
     */
    public function saveUser($data){

        $this->name = $data->mobile;
        $this->real_name = $data->cnName;
        $this->password = '';
        $this->status = self::ACTIVE;
        $this->source = self::EXT;
        $this->email = encrypt_func('',$this->name);

        $this->phone = encrypt_func($data->mobile,$this->name);
        return $this->save();
    }
    /**
     * 用户基本信息
     * @param int $id
     * @return mixed
     */
    public function getBaseInfo ($id){
        //fields
        $select = ['users.id','users.real_name','users.email','users.phone','user_ext.alias','user_ext.sex','user_ext.birth','user_ext.team','user_ext.home_page','user_ext.wechat','user_ext.qq','user_ext.avatar','users.created_at'];
        $builder = $this->select($select);

        //where
        if (isset($id) && $id >= 0){

            $builder = $builder->where('users.id',$id);
        }
        $builder = $builder->leftJoin('user_ext',function ($join){
            $join->on("users.id","=","user_ext.user_id");
        });
        $info = $builder->first();
        //空头像返回null
        if (empty($info->avatar)) {
            $info->avatar = null;
        } else {

            $info->avatar = asset($info->avatar);
        }
        //解密
        $info->email = decrypt_func($info->email,$info->name);
        $info->phone = decrypt_func($info->phone,$info->name);

        return $info;
    }
}
